package com.HZNU.Controller;

import com.HZNU.Service.DeptService;
import com.HZNU.spring_api.POJO.Dept;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient; //有netfix和spring的，这里选spring 获取服务信息

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") int id){
        return deptService.selectByPrimaryKey(id);
    }

    //获取注册进微服务的清单,主要是团队协作的时候 去看信息的，并且需要在服务类中开启注解 @EnableDiscoveryClient
    @GetMapping("/dept/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        System.out.println("信息是"+services);

        //通过微服务id 可以拿到 application
        List<ServiceInstance> instances = discoveryClient.getInstances("HZNU-XIAODONG-SPRINGCLOUD-PROVIDER-DEPT");
        for(ServiceInstance instance : instances){
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()
            );
        }
        return this.discoveryClient;
    }


}
